import traceback
from django.middleware.common import MiddlewareMixin
from utils.utils import R 
from utils.code import StatusCodeEnum 
from utils.log import logging
from config.config import SysConfig


class LoginIntercept(MiddlewareMixin):
    
    
    
    def process_request(self,request):
        
        url_method = "{}{}".format(request.path,request.method)
        
        if url_method not in SysConfig.WHITE_URL_LIST:
            public_key = request.session.get("public_key",None)
            if not public_key:
                return R.other_result(StatusCodeEnum.NO_LOGIN_ERR)
            


class ExceptionIntercept(MiddlewareMixin):
    
    def process_exception(self,request,exception):
        traceback_info = traceback.format_exc()
        logging.error(traceback_info)
        
        return R.server_error()